import React from 'react'
import { Table, Popconfirm, message } from 'antd'
import PropTypes from 'prop-types'
import './manager.less'
import API from '../../api/person/person'
import ManagerViewer from './ManagerViewer'
import DistributeViewer from './DistributeViewer'

class IcsComponentsIdleGrid extends React.Component {
    state = {
        showViewer: false,
        distributeViewer: false,
        job_status:[
            {value:1, name: '试用'},
            {value:10, name: '在职'},
            {value:20, name: '离职'}
        ],
        options: [],
        checkedArr: []
    }
    componentDidMount () {
    }
    resetPassword = (user_name) => {
        API.resetPassword({user_name: user_name}).then(res => {
            if (res.error === 0) {
                message.success('重置成功！')
            } else {
                message.error(res.message || '重置失败！')
            }
        })
    }
    editManager = (row) => {
        this.setState({showViewer: true, rowInfo: row, isEditing: true})
    }
    doViewer = () => {
        this.setState({showViewer: false});
    }
    doDistributeViewer= () => {
        this.setState({distributeViewer: false});
    }
    managerRights = (row) => {
        this.setState({distributeViewer: true, rowInfo: row});
        this.getAllRightsList(row.user_id)
    }
    getAllRightsList = (id) => {
        API.getAllRightList({user_id:id}).then(res => {
            console.log(res.data.listData)
            if (res.error === 0) {
                let arr = []
                let checkedArr = []
                for (let i = 0; i < res.data.listData.length; i++) {
                    arr.push({label: res.data.listData[i].pri_name, value: res.data.listData[i].pri_code })
                    if (res.data.listData[i].checked) {
                        checkedArr.push(res.data.listData[i].pri_code)
                    }
                }
                this.setState({options: arr, checkedArr: checkedArr})
            }
        })
    }
    render () {
        return (
            <div className="manager-grid">
                <Table
                    pagination={false}
                    bordered
                    dataSource={this.props.list}
                    rowKey="user_id">
                    <Table.Column
                        title="用户ID"
                        width={120}
                        dataIndex="user_id"
                    />
                    <Table.Column
                        title="姓名"
                        dataIndex="real_name"
                        width={150}
                    />
                    <Table.Column
                        title="账号"
                        width={120}
                        dataIndex="user_name"
                    />
                    <Table.Column
                        title="联系电话"
                        width={120}
                        dataIndex="user_tel"

                    />
                    <Table.Column
                        title="在职状态"
                        width={150}
                        dataIndex="job_status"
                        render={(text, row) => {
                            let a = parseInt(text, 10)
                            if (a === 1) {
                                return '试用'
                            } else if (a === 10) {
                                return '在职'
                            } else if (a === 20) {
                                return '离职'
                            }
                        }}
                    />
                    <Table.Column
                        title="操作"
                        width={150}
                        render={(text, row) => {
                            return <div>
                                <a onClick={(() => this.editManager(row))}>编辑</a>&emsp;
                                <Popconfirm title="确定要重置密码吗?" onConfirm={() =>this.resetPassword(row.user_name)} okText="Yes" cancelText="No">
                                    <a>重置密码</a>
                                </Popconfirm>&emsp;
                                <a onClick={() => this.managerRights(row)}>权限管理</a>
                            </div>
                        }}
                    />
                </Table>
                <ManagerViewer showViewer={this.state.showViewer} doSearch={this.props.doSearch} doViewer={this.doViewer} isEditing={this.state.isEditing} rowInfo={this.state.rowInfo}/>
                <DistributeViewer options={this.state.options} checkedArr={this.state.checkedArr} showViewer={this.state.distributeViewer}  doSearch={this.props.doSearch} doViewer={this.doDistributeViewer}  rowInfo={this.state.rowInfo}></DistributeViewer>
            </div>
        )
    }
}

IcsComponentsIdleGrid.propTypes = {
    onCreate: PropTypes.func,
    onIdleDetail: PropTypes.func,
    doSearch: PropTypes.func,
    grid: PropTypes.any,
    list: PropTypes.array
}
export default IcsComponentsIdleGrid
